sysroot: add a builder object
authorLuca BRUNO <luca.bruno@coreos.com>
Wed, 27 Oct 2021 09:45:34 +0000 (09:45 +0000)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:57 +0000 (12:53 -0400)
This adds a `SysrootBuilder` in order to allow consumers to load
a configured `Sysroot` in an ergonomic way. It tries to prevent
logic bugs coming from handling half-initialized entities.

rust-bindings/rust/src/lib.rs
rust-bindings/rust/src/sysroot.rs [new file with mode: 0644]

index 41cdd58bd4f88bd75d6720e8b09c5cc23dd2c2c3..5bce2adb64f2d72f819314d6669236b13311b1e4 100644 (file)
@@ -33,6 +33,8 @@ mod checksum;
 pub use crate::checksum::*;
 mod core;
 pub use crate::core::*;
+mod sysroot;
+pub use crate::sysroot::*;
 
 #[cfg(any(feature = "v2018_6", feature = "dox"))]
 mod collection_ref;
diff --git a/rust-bindings/rust/src/sysroot.rs b/rust-bindings/rust/src/sysroot.rs
new file mode 100644 (file)
index 0000000..7e207f3
--- /dev/null
@@ -0,0 +1,48 @@
+use crate::gio;
+use crate::Sysroot;
+use std::path::PathBuf;
+
+#[derive(Clone, Debug, Default)]
+/// Builder object for `Sysroot`.
+pub struct SysrootBuilder {
+    path: Option<PathBuf>,
+    mount_namespace_in_use: bool,
+}
+
+impl SysrootBuilder {
+    /// Create a new builder for `Sysroot`.
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    /// Set the path to the sysroot location.
+    pub fn path(mut self, path: Option<PathBuf>) -> Self {
+        self.path = path;
+        self
+    }
+
+    #[cfg(any(feature = "v2020_1", feature = "dox"))]
+    #[cfg_attr(feature = "dox", doc(cfg(feature = "v2020_1")))]
+    /// Set whether the logic is running in its own mount namespace.
+    pub fn mount_namespace_in_use(mut self, mount_namespace_in_use: bool) -> Self {
+        self.mount_namespace_in_use = mount_namespace_in_use;
+        self
+    }
+
+    /// Finalize this builder into a `Sysroot`.
+    pub fn build(self, cancellable: Option<&gio::Cancellable>) -> Result<Sysroot, glib::Error> {
+        let sysroot = {
+            let opt_file = self.path.map(|p| gio::File::for_path(p));
+            Sysroot::new(opt_file.as_ref())
+        };
+
+        #[cfg(feature = "v2020_1")]
+        if self.mount_namespace_in_use {
+            sysroot.set_mount_namespace_in_use();
+        }
+
+        sysroot.load(cancellable)?;
+
+        Ok(sysroot)
+    }
+}